package com.gxa.xc.rbac.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.xc.rbac.dto.ResultDTO;
import com.gxa.xc.rbac.mapper.AdminMapper;
import com.gxa.xc.rbac.pojo.Admin;
import com.gxa.xc.rbac.service.AdminService;
import com.gxa.xc.rbac.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
@SuppressWarnings("all")
public class AdminServiceImpl  implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public ResultDTO list() {

        List<Admin> all =adminMapper.findAll();
        return new ResultDTO(200,"success",all) ;
    }

    /**
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResultDTO list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Admin> all =adminMapper.findAll();
        /*将结果包装成pageInfo*/
        PageInfo<Admin> pageInfo = new PageInfo<>(all);
        return new ResultDTO(200,"success",pageInfo) ;

    }

    @Override
    public ResultDTO add(Admin admin) {
        /*密码加密*/
        admin.setAdminPwd(MD5Util.MD55(admin.getAdminPwd()));
        /*初始化添加时间*/
        admin.setCreateTime(new Timestamp(System.currentTimeMillis()));
        adminMapper.save(admin);

        return new ResultDTO(200,"添加成功");
    }

}
